<template>
  <a-modal title-align="start" title="移交任务" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="400">
    <a-form :model="form" ref="formRef" auto-label-width>
      <a-form-item label="移交对象" field="userId" :rules="[{ required: true, message: '请选择终止原因' }]">
        <a-select @change="handleUserChange" v-model="form.userId" placeholder="请选择" allow-search>
          <a-option v-for="item of userList" :key="item.id" :label="item.userName" :value="item.id"></a-option>
        </a-select>
      </a-form-item>
    </a-form>
    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="loading" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { defineComponent, ref, computed, onMounted } from 'vue'
  import { transferTask } from '@/api/task'
  import { getOrganizesUser } from '@/api/system'
  import { Message } from '@arco-design/web-vue'
  let props = defineProps(['modelValue', 'type', 'ids'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })
  const formRef = ref()
  let form: any = ref({})
  let loading = ref(false)
  let userList: any = ref([])
  const submit = async () => {
    const valid = await formRef.value.validate()
    if (valid) return
    let params = {
      executer: form.value.executer,
      idList: props.ids,
    }

    loading.value = true
    try {
      const res: any = await transferTask(params)
      if (res) {
        showvisible.value = false
        Message.success('任务移交成功')
        emits('save')
      }
    } catch (error) {}
    loading.value = false
  }

  const getUserList = async () => {
    let params = {
      depart: false,
      pageNum: 1,
      pageSize: 1000,
    }
    const res: any = await getOrganizesUser(params)
    userList.value = res.data?.records || []
  }

  const handleUserChange = (id: string) => {
    let userInfo = userList.value.find((item: any) => item.id == id)
    let executer = {
      id: userInfo.id,
      name: userInfo.userName,
    }
    form.value.executer = executer
  }

  onMounted(() => {
    getUserList()
  })
</script>

<style lang="less" scoped></style>
